package logic

import (
	"context"
	"time"

	"paper-pro/app/assistant/rpc/internal/svc"
	"paper-pro/app/assistant/rpc/pb"
	"paper-pro/pkg/xerr"

	"github.com/pkg/errors"
	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateAssistantLastUseAtLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUpdateAssistantLastUseAtLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateAssistantLastUseAtLogic {
	return &UpdateAssistantLastUseAtLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UpdateAssistantLastUseAtLogic) UpdateAssistantLastUseAt(
	in *pb.UpdateAssistantLastUseAtReq) (*pb.UpdateAssistantLastUseAtResp, error) {
	lastUseAt := time.Unix(in.LastUseAt, 0)
	_, err := l.svcCtx.AssistantModel.UpdateLastUseAt(l.ctx, in.Id, in.UserId, lastUseAt)
	if err != nil {
		return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "修改助ID：%s理最后使用时间：%d失败，err: %v", in.Id, err, in.LastUseAt)
	}

	return &pb.UpdateAssistantLastUseAtResp{}, nil
}
